package com.meitianhui.convenitenbuy.module.store.presenter;

import com.google.gson.Gson;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.base.BaseResponse;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.http.ResponseObserver;
import com.meitianhui.convenitenbuy.main.model.bean.UserToken;
import com.meitianhui.convenitenbuy.module.order.model.bean.EmptyInfo;
import com.meitianhui.convenitenbuy.module.store.contract.OneLotteryYYYContract;
import com.meitianhui.convenitenbuy.module.store.http.StoreApiDataSource;
import com.meitianhui.convenitenbuy.module.store.model.PayInfoBean;
import com.meitianhui.convenitenbuy.module.store.model.StoreOneLotteryBean;
import com.meitianhui.convenitenbuy.module.store.model.YYYLuckBean;
import com.meitianhui.convenitenbuy.module.store.ui.activity.OneLotteryYYYActivity;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by Administrator on 2017/11/27.
 */

public class OneLotteryYYYPresenter implements OneLotteryYYYContract.Presenter {

    private OneLotteryYYYActivity mContext;
    private final OneLotteryYYYContract.View mView;
    private Gson gson;

    public OneLotteryYYYPresenter(OneLotteryYYYActivity view,
                                  OneLotteryYYYActivity context) {
        this.mView = view;
        this.mContext = context;
        gson = new Gson();
    }

    @Override
    public void attachView(Object view) {
    }

    @Override
    public void detachView() {
    }

    @Override
    public void getYYYLuckData(StoreOneLotteryBean.ActivityListEntity entity, PayInfoBean bean) {
        UserToken token = InfoProvider.getUserToken(App.getInstance());
        Map<String, String> map = new HashMap<>();
        map.put("activity_id", entity.getActivity_id());
        // map.put("activity_id","e28fa6feea9a485bbba3771afd8c005c");
        map.put("consumer_id", token.getMember_id());
        map.put("draw_token", bean.pay_info.get("draw_token"));
        String params = gson.toJson(map);

        StoreApiDataSource.getInstance()
                .getYYYData(token.getUser_token(), "goods.ldYyyActivityLuckDrawFind", params)
                .subscribe(new ResponseObserver<BaseResponse<YYYLuckBean>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<YYYLuckBean> response) {
                        mView.getYYYLuckDataBack(true, null, response.data);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        mView.getYYYLuckDataBack(false, msg, null);
                    }
                });
    }

    @Override
    public void postLuckMsg(YYYLuckBean bean) {
        Map<String, String> map = new HashMap<>();
        map.put("process_id", bean.getProcess_id());
        map.put("draw_code", bean.getLuck_code());
        String params = gson.toJson(map);

        StoreApiDataSource.getInstance()
                .postLuckMsg(InfoProvider.getUserToken(App.getInstance()).getUser_token(),
                        "goods.ldActivtiyWinning", params)
                .subscribe(new ResponseObserver<BaseResponse<EmptyInfo>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<EmptyInfo> response) {
                        mView.postLuckMsgBack(true, null);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        mView.postLuckMsgBack(false, msg);
                    }
                });
    }

    @Override
    public void postFavoriteStore(String storeId) {
        Map<String, String> map = new HashMap<>();
        map.put("stores_id", storeId);
        map.put("consumer_id", InfoProvider.getUserToken(App.getInstance()).getMember_id());
        String params = gson.toJson(map);

        StoreApiDataSource.getInstance()
                .operationCollect(InfoProvider.getAppToken(App.getInstance()),
                        "stores.consumer.favoriteStore", params)
                .subscribe(new ResponseObserver<BaseResponse<EmptyInfo>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<EmptyInfo> response) {
                        mView.postFavoriteStoreBack(true, null);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        mView.postFavoriteStoreBack(false, msg);
                    }
                });
    }

}
